package p2p.view.components.bases
{
	import flash.events.MouseEvent;
	
	import mx.containers.VBox;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.TextArea;
	import mx.controls.TextInput;
	import mx.formatters.DateFormatter;
	import mx.utils.StringUtil;
	
	import p2p.controller.events.FileSharingEvent;
	import p2p.controller.events.MessageEvent;
	import p2p.data.CallerData;
	import p2p.model.vo.ClientVO;
	import p2p.model.vo.MessageVO;
	import p2p.utils.MessageUtils;
		
	public class ChatBoxBase extends VBox
	{
		[Bindable]
		public var msgTI:TextInput;
		[Bindable]
		public var msgsArea:TextArea;
		[Bindable]
		public var sendBtnPrivate:Button;
		[Bindable]
		public var sendBtnAll:Button;
		
		[Bindable]
		public var browseFSBtn:Button;
		
		private var formatter:DateFormatter;
		
		/**
		 * Constructor
		 */
		public function ChatBoxBase()
		{
			super();
			formatter = new DateFormatter();
			formatter.formatString = "J:NN:SS";
		}
		
		private var _caller:CallerData;
		
		public function set caller(value:CallerData) : void
		{
			_caller = value;
			_caller.addEventListener(MessageEvent.MESSAGE_EVENT, receiveMessageE);
		}
		
		override protected function createChildren() : void
		{
			super.createChildren();
			sendBtnPrivate.addEventListener(MouseEvent.CLICK, onSendBtnPrivate);
			sendBtnAll.addEventListener(MouseEvent.CLICK, onSendBtnAll);
			browseFSBtn.addEventListener(MouseEvent.CLICK, onBrowseFSBtnClick);
		}
		
		private function onSendBtnPrivate(e:MouseEvent) : void
		{
			var msgText:String = StringUtil.trim(msgTI.text);
			var msgRecivTxt:String = MessageUtils.getPrivateRecieveMessage(_caller.caller.selfName, msgText);
			var atLeastOneSelected:Boolean;
			for each (var client:ClientVO in _caller.clients)
			{
				if (client.subscribedForPrivateMsg)
				{
					var clientName:String = _caller.getClientNameBySessionKey(client.sessionKey)
					var msgOwnerTxt:String = MessageUtils.getPrivateSendMessage(clientName, msgText);
					var msg:MessageVO = new MessageVO(msgRecivTxt, msgOwnerTxt, client.sessionKey, _caller.caller.selfKey);
					
					_caller.sendPrivateMessage(msg);
					receiveMessage(msg);
					atLeastOneSelected = true;
				}
			}
			if (atLeastOneSelected)
			{
				cleanMessageField();
			}
			else
			{
				Alert.show("At least one user should be selected for Private Distribution.");
			}
		}
		
		protected function onSendBtnAll(e:Event) : void
		{
			var msgText:String = StringUtil.trim(msgTI.text);
			var msgRecipTxt:String = MessageUtils.getPublicRecieveMessage(_caller.caller.selfName, msgText);
			var msgOwnerTxt:String = MessageUtils.getPublicSendMessage(msgText);
			var msg:MessageVO = new MessageVO(msgRecipTxt, msgOwnerTxt, null, _caller.caller.selfKey);
			_caller.sendGroupMessage(msg);
			receiveMessage(msg);
			cleanMessageField();
		}
		
		private function cleanMessageField() : void
		{
			msgTI.text = "";
		}
		
		private function onBrowseFSBtnClick(event:MouseEvent) : void
		{
			event.stopPropagation();
			dispatchEvent(new FileSharingEvent(FileSharingEvent.BROWSE_FILE_SYSTEM));
		}
		
		private function receiveMessageE(event:MessageEvent) : void
		{
			var msg:MessageVO = event.message;
			receiveMessage(msg);
		}
		
		private function receiveMessage(msg:MessageVO) : void
		{
			var time:String = formatter.format(new Date());
			if (_caller.caller.selfKey == msg.ownerSessionKey)
			{
				msgsArea.htmlText += time + "  " + msg.ownerMessage;
			}
			else
			{
				msgsArea.htmlText += time + "  " + msg.recipientMessage;
			}
			msgsArea.verticalScrollPosition = msgsArea.textHeight + 10;
			msgsArea.validateNow();
		}
	}
}